/**
 * 刷新验证码图片
 * @param obj html对象
 * @param imgurl ： 图片的url
 */
function refreshAuthimg(obj,imgurl){
	
	obj.attr('src',imgurl + '?' + Math.random());
}

function userLogin(){
	
	var uname = $('#username').val();
	var pwd = $('#password').val();
	var authcode = $('#authcode').val();
	var cbackurl = $('#callbackurl').val();

	var errmsg = '';
	var suc = false;
	if(!uname){
		errmsg = '请填写用户名！';
	}else if(!pwd){
		errmsg = '请填写密码！';
		
	}else if(!authcode){
		errmsg = '请填写验证码！';
	}else{
		suc = true;
	}
	
	if(!suc){
		//不成功出现提示弹窗
		layer.open({
		  title: '消息',
		  content: errmsg
		});   
		return false;
		
	}else{
		return true;
		
	}
	
}

function userReg(){
	
	var phone = $('#phone').val();
	var pwd = $('#password').val();
	var mobilecode = $('#mobilecode').val();
	var uniqid = $('#uniqid').val();
	var authcode = $('#authcode').val();
	var agreement = $('#agreement').is(':checked');
	
	
	var suc = false;
	if(!agreement){
		errmsg = '请阅读中华户外网协议！';
	}else if(!phone){
		errmsg = '请填写手机号！';
	}else if(!pwd){
		errmsg = '请填写密码！';
		
	}else if(!mobilecode){
		errmsg = '请填写手机验证码！';
	}else{
		suc = true;
	}
	
	if(!suc){
		layer.open({
		  title: '消息',
		  content: errmsg
		}); 
		return false;
	}else{
		/*var reg_form = $('#reg_form');
		reg_form.submit();*/
		return true;
	}
	
}

function userSms(){
	
	var phone = $('#phone').val();
	var authcode = $('#authcode').val();
	
	var errmsg = '';
	if(!phone){
		errmsg = "请输入手机号！";
		layer.open({
		  title: '消息',
		  content: errmsg
		}); 
		
	}else if(!authcode){
		errmsg = "请输入验证码！";
		layer.open({
		  title: '消息',
		  content: errmsg
		}); 
	}else{
		var params = {'phone':phone,'authcode':authcode};

		$.get('/user/sms/reg',
			params, 
		    function(result) {
			
			    var status = result.status;
			    var msg = result.message;
			    if(2000 == status){ //短信发送成功
			    	var uniqid = result.uniqid;
			    	$('#uniqid').val(uniqid);
			    	tips('短信验证码发送成功！',closesnd = 2);
			    	smsover(60);
			    	
			    }else{
			    	layer.open({
					  title: '消息',
					  content: result.message
					}); 
			    }
		     }
		     ,'json'
		);
	}
	
}

function userBindSms(){
	
	var phone = $('#phone').val();
	var user_exist = $('#user_exist').val();
	
	var errmsg = '';
	if(!phone){
		errmsg = "请输入手机号！";
		layer.open({
		  	title: '消息',
		  	content: errmsg
		}); 
		return false;
	}
	
	if(user_exist == 'y'){ //如果是老用户,判断手机号是否已经注册
		
		var params = {'phone':phone};
		
		$.post('/user/regcheck',
			params, 
		    function(result) {
			
			    var status = result.status;
			    var msg = result.message;
			    if('2006' == status){ //输入的手机号,已经绑定
			    	
			    	var d = dialog({
			    	    title: '提示',
			    	    content: '该手机号已经绑定,继续操作将会解除此号码与其他账号的绑定关系！',
			    	    okValue: '确定',
			    	    ok: function () {
			    	    	sendBindSms(phone);
			    	        return true;
			    	    },
			    	    cancelValue: '取消',
			    	    cancel: function () {}
			    	});
			    	d.show();
			    }else{
			    	sendBindSms(phone);
			    }
		     }
		     ,'json'
		);
		
	}else{ 
		sendBindSms(phone);
	}
	
}

function sendBindSms(phone){
	
	var params = {'phone':phone};
	
	$.get('/user/sms/bind',
		params, 
	    function(result) {
		
		    var status = result.status;
		    var msg = result.message;
		    if(2000 == status){ //短信发送成功
		    	var uniqid = result.uniqid;
		    	$('#uniqid').val(uniqid);
		    	tips('短信验证码发送成功！',closesnd = 2);
		    	smsover(60);
		    	
		    }else{
		    	layer.open({
				  	title: '消息',
				  	content: result.message
				}); 		    	
		    }
	     }
	     ,'json'
	);
}


function userResetpwdSms(){
	
	var phone = $('#phone').val();
	var errmsg = '';
	if(!phone){
		errmsg = "请输入手机号！";
		layer.open({
		  	title: '消息',
		  	content: errmsg
		}); 
	}else{
		var params = {'phone':phone};
		
		$.get('/user/sms/resetpwd',
			params, 
		    function(result) {
			
			    var status = result.status;
			    var msg = result.message;
			    if(2000 == status){ //短信发送成功
			    	var uniqid = result.uniqid;
			    	$('#uniqid').val(uniqid);
			    	tips('短信验证码发送成功！',closesnd = 2);
			    	smsover(60);
			    	
			    }else{
			    	layer.open({
					  	title: '消息',
					  	content: result.message
					}); 
			    	
			    }
		     }
		     ,'json'
		);
	}
	
}

function userBind(){

	var openid = $('#openid').val();
	var unionid = $('#unionid').val();
	var regfrom = $('#regfrom').val();
	var nickname = $('#nickname').val();
	var user_exist = $('#user_exist').val();
	
	var phone = $('#phone').val();
	var mobilecode = $('#mobilecode').val();
	var uniqid = $('#uniqid').val();
	
	var suc = false;
	if(!phone){
		errmsg = '请填写手机号！';
	}else if(!mobilecode){
		errmsg = '请填写手机验证码！';
	}else{
		suc = true;
	}
	
	if(!suc){
		layer.open({
		  	title: '消息',
		  	content: errmsg
		}); 
		
		
	}else{
		var bind_form = $('#bind_form');
		bind_form.submit();
	}
	
}

function userResetpwd(){

	var openid = $('#openid').val();
	var unionid = $('#unionid').val();
	var regfrom = $('#regfrom').val();
	var nickname = $('#nickname').val();
	
	var phone = $('#phone').val();
	var mobilecode = $('#mobilecode').val();
	var uniqid = $('#uniqid').val();
	var pwd = $('#password').val();
	
	var suc = false;
	if(!phone){
		errmsg = '请填写手机号！';
	}else if(!mobilecode){
		errmsg = '请填写手机验证码！';
	}else if(!pwd){
		errmsg = '请填写密码！';
	}else{
		suc = true;
	}
	
	if(!suc){
		layer.open({
		  	title: '消息',
		  	content: errmsg
		}); 
		
		
	}else{
		var resetpwd_form = $('#resetpwd_form');
		resetpwd_form.submit();
	}
	
}

//计数器 
var g_icounter = '';

/**
 * 短信发送成功后 前端页面处理
 */
function smsover(resecends){
	
	if(resecends == 0){
		return true;
	}
	var obj_sms = $('#getsms');
	obj_sms.attr('class','getcode regetcode');
	obj_sms.attr('disabled','disabled');
	$('#counter').val('' + resecends);
	g_icounter = setInterval("smscounter()",1000);
}


function smscounter(){
	var i = $('#counter').val();
	i = i ? i : '0';
	i = parseInt(i);
	var obj_sms = $('#getsms');
	if( i == 0){ //结束,停止计数器
		obj_sms.attr('class','getcode');
		obj_sms.removeAttr('disabled');
		clearInterval(g_icounter);
		obj_sms.val('获取短信验证码');
		
	}else{
		i = i - 1;
		$('#counter').val('' + i);
		obj_sms.val(i + '秒后重新获取');
		obj_sms.attr('class','getcode  regetcode');
		obj_sms.attr('disabled','disabled');
		
	}
}

